package org.dakuo.renrenTool;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
 * GPL v3.0 欢迎大家下载分享，只希望能注明出处和原作者
 * 
 * @author Dakuo msn:xymlem@hotmail.com email:dakuowang@gmail.com
 */
public class RenrenLogin {

	static String OrigURL = "http://home.renren.com/Home.do";// renren homepage
	// the for posting date
	static String PassportPostUrl = "http://passport.renren.com/PLogin.do";
	static String Domain = "renren.com";

	/**
	 * three steps of login to get cookie
	 * 
	 * @param email
	 * @param password
	 * @return String::cookie
	 */
	public static List<Cookie> login(String email, String password) {
		// 先建立一个客户端实例，将模拟一个浏览器

		try {

			/**
			 * Beginning set up link 链接
			 */
			DefaultHttpClient httpclient = new DefaultHttpClient();
			// 1st Post to password.renren.com
			HttpPost httpPost = new HttpPost(PassportPostUrl);

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("email", email));
			nvps.add(new BasicNameValuePair("password", password));
			nvps.add(new BasicNameValuePair("origURL", OrigURL));
			nvps.add(new BasicNameValuePair("domain", Domain));
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

			httpPost
					.setHeader(
							"User-Agent",
							"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)");
			httpPost
					.addHeader("Accept",
							"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			httpPost.addHeader("Accept-Language", "en-us,en;q=0.5");
			httpPost.addHeader("Accept-Encoding", "gzip,deflate");
			httpPost.addHeader("Accept-Charset",
					"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			httpPost.addHeader("Content-Type",
					"application/x-www-form-urlencoded");

			HttpResponse response = httpclient.execute(httpPost);
			HttpEntity entity = response.getEntity();

			System.out.println("Login post: " + response.getStatusLine());
			if (entity != null) {
				entity.consumeContent();
			}

			Header headerLocation = response.getFirstHeader("Location");
			// 2nd Get to login.renren.com

			HttpGet httpget = new HttpGet(headerLocation.getValue());
			response = httpclient.execute(httpget);
			entity = response.getEntity();

			System.out.println("Login get: " + response.getStatusLine());
			if (entity != null) {
				entity.consumeContent();
			}
			List<Cookie> cookiesList = httpclient.getCookieStore().getCookies();
			/**
			 * End 链接
			 */
			httpclient.getConnectionManager().shutdown();
			return cookiesList;

		} catch (Exception e) {

			System.out.println(e.getStackTrace());
			return null;
		}

	}

	public static void main(String[] args) {
		String email = "xymlem@hotmail.com";
		String password = "lemxym";
		if (login(email,password)!=null)
			System.out.print("connected");
		else
			System.out.print("fail");

	}

}
